/*=====================================================
Critical Links
Auto Configuration Server

Authors:
	Rui Eduardo Gouveia Gil - r-gil@critical-links.com

Date: 2011
Version 0.1
=======================================================*/
Device = function(xml) {
	var xmlDoc = parseXML(xml);
	
	this.type = 'd';
	
	this.id = xmlDoc.getElementsByTagName("ID")[0].childNodes[0].nodeValue;
	this.groupID = xmlDoc.getElementsByTagName("GroupID")[0].childNodes[0].nodeValue;
	this.configID = xmlDoc.getElementsByTagName("ConfigID")[0].childNodes[0].nodeValue;
	this.configMode = xmlDoc.getElementsByTagName("ConfigMode")[0].childNodes[0].nodeValue;
	this.groupName = xmlDoc.getElementsByTagName("GroupName")[0].childNodes[0].nodeValue;
	this.configName = xmlDoc.getElementsByTagName("ConfigName")[0].childNodes[0].nodeValue;	
	this.status = xmlDoc.getElementsByTagName("Status")[0].childNodes[0].nodeValue;
	this.statusStr = xmlDoc.getElementsByTagName("StatusStr")[0].childNodes[0].nodeValue;
	this.statusIcon = xmlDoc.getElementsByTagName("StatusIcon")[0].childNodes[0].nodeValue;
	this.updated = xmlDoc.getElementsByTagName("Updated")[0].childNodes[0].nodeValue;
	this.contacted = xmlDoc.getElementsByTagName("Contacted")[0].childNodes[0].nodeValue;
	this.creation = xmlDoc.getElementsByTagName("Creation")[0].childNodes[0].nodeValue;
	this.updatedStr = xmlDoc.getElementsByTagName("UpdatedStr")[0].childNodes[0].nodeValue;
	this.creationStr = xmlDoc.getElementsByTagName("CreationStr")[0].childNodes[0].nodeValue;
	this.contactedStr = xmlDoc.getElementsByTagName("ContactedStr")[0].childNodes[0].nodeValue;
	this.name = xmlDoc.getElementsByTagName("Name")[0].childNodes[0].nodeValue;
	
	if(this.configMode == 0)
		this.configModeName = "Automatic First Config";
	else if(this.configMode == 1)
		this.configModeName = "Force Automatic";
	else if(this.configMode == 2)
		this.configModeName = "Automatic";
	else
		this.configModeName = "Manual";

	if(xmlDoc.getElementsByTagName("RestoreConfigID").length != 0 && xmlDoc.getElementsByTagName("RestoreConfigID")[0].childNodes.length != 0)
		this.lastRestoreConfigID = xmlDoc.getElementsByTagName("RestoreConfigID")[0].childNodes[0].nodeValue;
	else
		this.lastRestoreConfigID = 0;
	
	if(xmlDoc.getElementsByTagName("RestoreConfigName").length != 0 && xmlDoc.getElementsByTagName("RestoreConfigName")[0].childNodes.length != 0)
		this.lastRestoreConfigName = xmlDoc.getElementsByTagName("RestoreConfigName")[0].childNodes[0].nodeValue;
	else
		this.lastRestoreConfigName = "";

	if(xmlDoc.getElementsByTagName("LastBackup").length != 0 && xmlDoc.getElementsByTagName("LastBackup")[0].childNodes.length != 0)
		this.lastBackup = xmlDoc.getElementsByTagName("LastBackup")[0].childNodes[0].nodeValue;
	else
		this.lastBackup = 0;

	if(xmlDoc.getElementsByTagName("LastRestore").length != 0 && xmlDoc.getElementsByTagName("LastRestore")[0].childNodes.length != 0)
		this.lastRestore = xmlDoc.getElementsByTagName("LastRestore")[0].childNodes[0].nodeValue;
	else
		this.lastRestore = 0;

	if(xmlDoc.getElementsByTagName("LastBackupStr").length != 0 && xmlDoc.getElementsByTagName("LastBackupStr")[0].childNodes.length != 0)
		this.lastBackupStr = xmlDoc.getElementsByTagName("LastBackupStr")[0].childNodes[0].nodeValue;
	else
		this.lastBackupStr = "";

	if(xmlDoc.getElementsByTagName("LastRestoreStr").length != 0 && xmlDoc.getElementsByTagName("LastRestoreStr")[0].childNodes.length != 0)
		this.lastRestoreStr = xmlDoc.getElementsByTagName("LastRestoreStr")[0].childNodes[0].nodeValue;
	else
		this.lastRestoreStr = "";

	if(xmlDoc.getElementsByTagName("Ip")[0].childNodes.length != 0)
		this.ip = xmlDoc.getElementsByTagName("Ip")[0].childNodes[0].nodeValue;
	else
		this.ip = "";

	if(xmlDoc.getElementsByTagName("SHA").length != 0 && xmlDoc.getElementsByTagName("SHA")[0].childNodes.length != 0)
		this.sha = xmlDoc.getElementsByTagName("SHA")[0].childNodes[0].nodeValue;
	else
		this.sha = "";
	
	if(xmlDoc.getElementsByTagName("Description")[0].childNodes.length != 0)
		this.description = xmlDoc.getElementsByTagName("Description")[0].childNodes[0].nodeValue;
	else
		this.description = "";
	
	if(xmlDoc.getElementsByTagName("ActionPeriod")[0].childNodes.length != 0)
		this.actionPeriod = xmlDoc.getElementsByTagName("ActionPeriod")[0].childNodes[0].nodeValue;
	else
		this.actionPeriod = "";

	if(xmlDoc.getElementsByTagName("Build").length != 0 && xmlDoc.getElementsByTagName("Build")[0].childNodes.length != 0)
		this.build = xmlDoc.getElementsByTagName("Build")[0].childNodes[0].nodeValue;
	else
		this.build = "";
	
	if(xmlDoc.getElementsByTagName("UpTime").length != 0 && xmlDoc.getElementsByTagName("UpTime")[0].childNodes.length != 0)
		this.upTime = xmlDoc.getElementsByTagName("UpTime")[0].childNodes[0].nodeValue;
	else
		this.upTime = "";

	if(xmlDoc.getElementsByTagName("Release").length != 0 && xmlDoc.getElementsByTagName("Release")[0].childNodes.length != 0)
		this.release = xmlDoc.getElementsByTagName("Release")[0].childNodes[0].nodeValue;
	else
		this.release = "";

	if(xmlDoc.getElementsByTagName("SwapUsed").length != 0 && xmlDoc.getElementsByTagName("SwapUsed")[0].childNodes.length != 0)
		this.swapUsed = xmlDoc.getElementsByTagName("SwapUsed")[0].childNodes[0].nodeValue;
	else
		this.swapUsed = "";

	if(xmlDoc.getElementsByTagName("MaxUsers").length != 0 && xmlDoc.getElementsByTagName("MaxUsers")[0].childNodes.length != 0)
		this.maxUsers = xmlDoc.getElementsByTagName("MaxUsers")[0].childNodes[0].nodeValue;
	else
		this.maxUsers = "";

	if(xmlDoc.getElementsByTagName("LicenseID").length != 0 && xmlDoc.getElementsByTagName("LicenseID")[0].childNodes.length != 0)
		this.licenseID = xmlDoc.getElementsByTagName("LicenseID")[0].childNodes[0].nodeValue;
	else
		this.licenseID = "";

	if(xmlDoc.getElementsByTagName("SystemLoad").length != 0 && xmlDoc.getElementsByTagName("SystemLoad")[0].childNodes.length != 0)
		this.systemLoad = xmlDoc.getElementsByTagName("SystemLoad")[0].childNodes[0].nodeValue;
	else
		this.systemLoad = "";

	if(xmlDoc.getElementsByTagName("GuiVersion").length != 0 && xmlDoc.getElementsByTagName("GuiVersion")[0].childNodes.length != 0)
		this.guiVersion = xmlDoc.getElementsByTagName("GuiVersion")[0].childNodes[0].nodeValue;
	else
		this.guiVersion = "";

	if(xmlDoc.getElementsByTagName("FreeMemory").length != 0 && xmlDoc.getElementsByTagName("FreeMemory")[0].childNodes.length != 0)
		this.freeMemory = xmlDoc.getElementsByTagName("FreeMemory")[0].childNodes[0].nodeValue;
	else
		this.freeMemory = "";

	if(xmlDoc.getElementsByTagName("PepsVersion").length != 0 && xmlDoc.getElementsByTagName("PepsVersion")[0].childNodes.length != 0)
		this.pepsVersion = xmlDoc.getElementsByTagName("PepsVersion")[0].childNodes[0].nodeValue;
	else
		this.pepsVersion = "";

	if(xmlDoc.getElementsByTagName("CustomerName").length != 0 && xmlDoc.getElementsByTagName("CustomerName")[0].childNodes.length != 0)
		this.customerName = xmlDoc.getElementsByTagName("CustomerName")[0].childNodes[0].nodeValue;
	else
		this.customerName = "";

	if(xmlDoc.getElementsByTagName("PartitionVar").length != 0 && xmlDoc.getElementsByTagName("PartitionVar")[0].childNodes.length != 0)
		this.partitionVar = xmlDoc.getElementsByTagName("PartitionVar")[0].childNodes[0].nodeValue;
	else
		this.partitionVar = "";

	if(xmlDoc.getElementsByTagName("PartitionRoot").length != 0 && xmlDoc.getElementsByTagName("PartitionRoot")[0].childNodes.length != 0)
		this.partitionRoot = xmlDoc.getElementsByTagName("PartitionRoot")[0].childNodes[0].nodeValue;
	else
		this.partitionRoot = "";

	if(xmlDoc.getElementsByTagName("PartitionHome").length != 0 && xmlDoc.getElementsByTagName("PartitionHome")[0].childNodes.length != 0)
		this.partitionHome = xmlDoc.getElementsByTagName("PartitionHome")[0].childNodes[0].nodeValue;
	else
		this.partitionHome = "";

	if(xmlDoc.getElementsByTagName("InstalledMemory").length != 0 && xmlDoc.getElementsByTagName("InstalledMemory")[0].childNodes.length != 0)
		this.installedMemory = xmlDoc.getElementsByTagName("InstalledMemory")[0].childNodes[0].nodeValue;
	else
		this.installedMemory = "";

	if(xmlDoc.getElementsByTagName("HardwareDescription").length != 0 && xmlDoc.getElementsByTagName("HardwareDescription").length != 0 && xmlDoc.getElementsByTagName("HardwareDescription")[0].childNodes.length != 0)
		this.hardwareDescription = xmlDoc.getElementsByTagName("HardwareDescription")[0].childNodes[0].nodeValue;
	else
		this.hardwareDescription = "";
	
	this.tid = this.type+this.id;
	this.text = this.name + " (" + this.statusStr + ")";	
};

Ext.reg('appdevice', Device);